package GUI;
import java.security.AllPermission;
import java.util.ArrayList;

import Control.Shape;

import processing.core.PImage;



public class TextBox
{
	public int x;
	public int y;
	public int width = 130;
	public String text;
	public ScrollBar scrollBar;
	public boolean active;
	public ArrayList<Shape> shapesToShow;
	public ShapeButtons sb;
	Button allShapes;
	Button noShapes;

	public float buttonTimer;
	public int heightVal;
	public int suggestionHeight;

	public  class ShapeButtons{
		public ArrayList<Button> shapesSuggestionButtons;
		public ArrayList<Shape> shapes;

		public ShapeButtons(){
			shapesSuggestionButtons = new ArrayList<Button>();
			shapes = new ArrayList<Shape>();
		}
	}


	////////////////////////////////////////////////////////////////////////////////////////////////////
	public TextBox()
	{
		text = "";
		scrollBar = new ScrollBar();
		active=false;
		shapesToShow = GV.shapes;
		sb = new ShapeButtons();
		allShapes = new Button("All Shapes");
		allShapes.x=x+5;
		noShapes = new Button("No Shape");
		noShapes.x=x+5;
		updateShapes();
	}

	public void draw()
	{
		GV.processing.rectMode(GV.processing.CORNER);

		GV.processing.fill(GV.backGroundColor);
		//GV.processing.fill(200);
		GV.processing.stroke(80);
		GV.processing.strokeWeight(2);
		GV.processing.rect(x, y, width, 30);

		GV.processing.fill(GV.textColor);	
		//GV.processing.textFont(GV.processing.plotFont, 14);
		GV.processing.textAlign(GV.processing.LEFT);


		int allNoRow =0;
		if(this.text.length()==0)
			allNoRow=1;
		suggestionHeight = 130 + allNoRow*50;

		if(active){
			if(buttonTimer==(float)0.0){
				scrollBar.val=0;
				buttonTimer = GV.timer;
				heightVal=0;
			}
			heightVal = (int)GV.processing.lerp(heightVal,suggestionHeight,(float) 0.3);
			drawSuggestions(allNoRow);
		}
		else{if(buttonTimer!=(float)0.0){heightVal=suggestionHeight;}
		GV.processing.text("Shapes...", x + 5, y + 20);
		heightVal = (int)GV.processing.lerp(heightVal,0,(float) 0.3);
		if(heightVal!=0)
		drawSuggestions(allNoRow);
		buttonTimer=(float)0.0;

		}

	}

	public void drawSuggestions(int allNoRow){
		GV.processing.text(text, x + 5, y + 20);
		GV.processing.fill(44,160);
		GV.processing.stroke(80);
		GV.processing.strokeWeight(1);
		GV.processing.rect(x, y+30, width, heightVal);


		scrollBar.width = 15;
		scrollBar.x =  x +width-15;
		scrollBar.y = y+30+allNoRow*50;
		scrollBar.height = heightVal-allNoRow*50-4;
		if(heightVal>scrollBar.y-y)
		scrollBar.draw();  
		scrollBar.size = 1/(float)(GV.shapes.size()-3);

		if(allNoRow==1){
			if(allShapes.state){
				GV.shapesPanel.addAllShapes();
				allShapes.state=false;
				this.active=false;
				text = new String();
			}
			else if(heightVal>30){allShapes.y=y+30;allShapes.draw();	}
			if(noShapes.state){
				GV.shapesPanel.removeAllShapes();
				noShapes.state=false;
				this.active=false;
				text = new String();
			}
			else if (heightVal>55){noShapes.y=y+30+25;noShapes.draw();}
		}

		showShapesButtons(allNoRow);
	}
	
	

	
	
	
	public void keyTyped()
	{
		if(GV.processing.key==GV.processing.ENTER){

			if(shapesToShow.size()>0){
				Shape s =shapesToShow.get(0);
				if(GV.shapesSelected.contains(s)){
					GV.shapesPanel.removeShape(s);
				}
				else{
					GV.shapesPanel.addShape(s);
				}
				this.active=false;
				text = new String();
			}


		}
		else if(GV.processing.key == 8)
		{
			if(text.length() > 0) text = text.substring(0, text.length() - 1);
		}
		else if(text.length()>16){
			text = text.substring(0, text.length() - 1);
			text += GV.processing.key;
		}
		else
		{
			text += GV.processing.key;
		}

		updateShapes();


	}



	public boolean mouseOver()
	{
		if(GV.processing.mouseX > x && GV.processing.mouseX < x + width && 
				GV.processing.mouseY > y && GV.processing.mouseY < y + 30) return true;
		return false;
	}

	public boolean mouseOverSuggestions(){

		if(GV.processing.mouseX > x && GV.processing.mouseX < x + width && 
				GV.processing.mouseY > y+30 && GV.processing.mouseY < y + 160+50)return true;
		return false;
	}

	//show  shapes buttons in the suggestions
	private void showShapesButtons(int allNoRow) {

		float j = Main.map (scrollBar.val,0,1,0,shapesToShow.size()-3);
		if(j<0)
			j=0;
		for(int i=(int)j;i<(int)j+5 && i<sb.shapesSuggestionButtons.size()&&i-(int)j<-allNoRow+heightVal/28-0.3;i++){
			Button b = sb.shapesSuggestionButtons.get(i);
			if(b.state){

				if(shapesToShow.size()>i){
					Shape s =shapesToShow.get(i);
					if(GV.shapesSelected.contains(s)){
						GV.shapesPanel.removeShape(s);
					}
					else{
						GV.shapesPanel.addShape(s);
					}
					this.active=false;
					text = new String();
				}

				updateShapes();
				break;
			}else{
				b.y=y+30+(i-(int)j)*25+allNoRow*50;
				b.draw();
			}
		}		
	}
	



	public void updateShapes(){
		System.out.println(text);
		shapesToShow = new ArrayList<Shape>();
		for(int i=0;i<GV.shapes.size();i++){
			if(GV.shapes.get(i).getName().toLowerCase().startsWith(text.toLowerCase())){
				shapesToShow.add(GV.shapes.get(i));
				//System.out.println("I add "+Utils.characters.get(i).getName());
			}
		}
		sb.shapesSuggestionButtons= new ArrayList<Button>();
		for(int i=0;i<shapesToShow.size();i++){
			Button b = new Button();
			b.setLabel(shapesToShow.get(i).getName());
			b.x=x+5;
			sb.shapesSuggestionButtons.add(b);
			sb.shapes.add(shapesToShow.get(i));
		}

	}



	////////////////////////////////////////////////////////////////////////////////////////////////////
	PImage icon;
	String label;
}